package net.borderwars.map;

import net.borderwars.robots.Robot;

import javax.swing.*;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashSet;

/**
 * @author Eric
 *         Date: Apr 30, 2005
 *         Time: 10:44:22 AM
 */
public class RobotStatusForm extends JPanel {
    private OurDataModel odm;

    public RobotStatusForm (ArrayList<net.borderwars.robots.Robot> robots) throws HeadlessException {
        setSize (400, 300);
        setLayout (new BorderLayout ());
        JTable data = new JTable ();

        data.setAutoCreateColumnsFromModel (true);
        //  JScrollPane scrollpane = new JScrollPane(data);
        odm = new OurDataModel (robots);
        data.setModel (odm);
        add (data, BorderLayout.CENTER);
        add (data.getTableHeader (), BorderLayout.NORTH);
        data.doLayout ();

    }

    public void add (Robot r) {
        odm.add (r);
    }

    public void redraw () {
        odm.fireBigChange ();
    }

    public class OurDataModel extends AbstractTableModel {
        private String names[] = {"ID", "Dmg", "Dir", "Speed", "Scan", "X", "Y"};
        HashSet<TableModelListener> listeners = new HashSet<TableModelListener> ();
        private ArrayList<net.borderwars.robots.Robot> robots;

        public OurDataModel (ArrayList<net.borderwars.robots.Robot> robots) {
            this.robots = robots;
        }

        public int getRowCount () {
            return (robots.size ());
        }

        //
        public int getColumnCount () {
            return (names.length);
        }

        //
        public String getColumnName (int columnIndex) {
            return (names[columnIndex]);
        }
//

        public Class getColumnClass (int c) {
            return getValueAt (0, c).getClass ();
        }
//
//        public boolean isCellEditable(int rowIndex, int columnIndex) {
//            return false;
//        }

        //

        public Object getValueAt (int rowIndex, int columnIndex) {
            net.borderwars.robots.Robot r = robots.get (rowIndex);
            switch (columnIndex) {
                case 0:
                    String rtn = r.getVM ().getName ();
                    rtn = rtn.substring (rtn.lastIndexOf ('.') + 1);
                    return (rtn);
                case 1:
                    return (r.damage ());
                case 2:
                    return (r.direction ());
                case 3:
                    return (r.speed ());
                case 4:
                    return (r.getLastScan ());
                case 5:
                    return (r.loc_x ());
                case 6:
                    return (r.loc_y ());
            }
            return (null);
        }
//
//        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//
//        }
//
//        public void addTableModelListener(TableModelListener l) {
//            listeners.add(l);
//        }
//
//        public void removeTableModelListener(TableModelListener l) {
//            listeners.remove(l);
//        }
//

        //

        public void add (Robot r) {
            robots.add (r);
        }
//

        private void fireBigChange () {
            super.fireTableDataChanged ();
//            TableModelEvent tme = new TableModelEvent(this, 0, robots.size());
//            for (TableModelListener tml : listeners) {
//                tml.tableChanged(tme);
//            }
        }
    }

}
